package me.spring.cloud.components.starter.security.access.configuration;

import me.spring.cloud.components.starter.oauth2.url.properties.IgnoreUrlProperties;
import me.spring.cloud.components.starter.security.access.condition.EnableResourceServerCondition;
import me.spring.cloud.components.starter.security.access.condition.EnableWebSecurityCondition;
import me.spring.cloud.components.starter.security.access.entrypoint.AuthExceptionEntryPoint;
import me.spring.cloud.components.starter.security.access.handler.AuthAccessDeniedHandler;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

/**
 * @author luffy
 */
@Slf4j
@Configuration
public class SecurityAccessAutoConfiguration {

  @Autowired
  private IgnoreUrlProperties ignoreUrlProperties;

  @Configuration
  @NoArgsConstructor
  @Conditional(EnableResourceServerCondition.class)
  private class ResourceServerSecurityAccessConfiguration extends ResourceServerConfigurerAdapter {
    //TODO 待实现根据配置文件实现动态变动
    @Override
    public void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
      if (ignoreUrlProperties.isEnable() && ignoreUrlProperties.getUrls().size() > 0) {
        http.authorizeRequests().antMatchers(
            ignoreUrlProperties.getUrls().toArray(new String[ignoreUrlProperties.getUrls().size()]))
            .permitAll();
      }
      http.authorizeRequests().anyRequest().authenticated().filterSecurityInterceptorOncePerRequest(false);
      log.info("Security Access Control is enabled on Resource Server Application");
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
      resources.authenticationEntryPoint(new AuthExceptionEntryPoint())
          .accessDeniedHandler(new AuthAccessDeniedHandler());
    }

  }

  @Configuration
  @NoArgsConstructor
  @Conditional(EnableWebSecurityCondition.class)
  private class WebSecurityAccessConfiguration extends WebSecurityConfigurerAdapter {
    //TODO 待实现根据配置文件实现动态变动
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
      if (ignoreUrlProperties.isEnable() && ignoreUrlProperties.getUrls().size() > 0) {
        http.authorizeRequests().antMatchers(
            ignoreUrlProperties.getUrls().toArray(new String[ignoreUrlProperties.getUrls().size()]))
            .permitAll();
      }
      http.authorizeRequests().anyRequest().authenticated().filterSecurityInterceptorOncePerRequest(false);
      log.info("Security Access Control is enabled on Web Application");
    }
  }

}
